/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * pnlRegistro.java
 *
 * Created on 16-nov-2011, 23:00:00
 */
package Comun;

import Controlador.Controladores.FuenteTabla;
import Controlador.Interfaz.DataFuente;
import javax.swing.JFrame;
import javax.swing.SwingWorker;

/**
 *
 * @author luisramirezmartinez
 */
public class pnlRegistro extends PanelRoundTranslucido {
    private FuenteTabla FT=null;
    private SamiFrame Vista=null;
    private pnlNavegacion nav=null;
    private PanelRoundTranslucido editor=null;
    private String tabla="";
    private boolean esNuevo=false;
    private JFrame parent=null;
    private pnlFiltro filtro;

    /** Creates new form pnlRegistro */
    public pnlRegistro() {
        initComponents();
    }

   public boolean isTodoOK(){
        boolean resultado =true;
        try{
        for (DataFuente object : this.getFT().getLstObservadores())  resultado=resultado && object.validar();
        } catch (Exception ex){
            resultado=false;
        }
                
        
        
        return resultado; 
    }

    private void Limpiar(){
         for (DataFuente object : this.getFT().getLstObservadores()) object.Limpiar();
    }
    
     private void estadoEdicion(){
        this.getNav().setVisible(false);
        this.samiButton1.setEnabled(false);
        this.samiButton2.setEnabled(false);
        this.samiButton5.setEnabled(false);
    
    }
     
   public void estadoNormal(){
            this.getNav().setVisible(true);
            this.samiButton1.setEnabled(true);
            this.samiButton2.setEnabled(true);
            this.samiButton4.setEnabled(true);
            this.samiButton5.setEnabled(true);
            //this.getFT().Consultar();
            //this.getVista().Refrescar();
    
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        samiButton2 = new Comun.SamiButton();
        samiButton3 = new Comun.SamiButton();
        samiButton4 = new Comun.SamiButton();
        samiButton5 = new Comun.SamiButton();
        samiButton1 = new Comun.SamiButton();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        samiButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus 2.png"))); // NOI18N
        samiButton2.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus.png"))); // NOI18N
        samiButton2.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus 2.png"))); // NOI18N
        samiButton2.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus.png"))); // NOI18N
        samiButton2.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_plus.png"))); // NOI18N
        samiButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton2MouseClicked(evt);
            }
        });
        add(samiButton2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        samiButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder 2.png"))); // NOI18N
        samiButton3.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder.png"))); // NOI18N
        samiButton3.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder 2.png"))); // NOI18N
        samiButton3.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder.png"))); // NOI18N
        samiButton3.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_folder.png"))); // NOI18N
        samiButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton3MouseClicked(evt);
            }
        });
        add(samiButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(50, 10, -1, -1));

        samiButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont 2.png"))); // NOI18N
        samiButton4.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont.png"))); // NOI18N
        samiButton4.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont 2.png"))); // NOI18N
        samiButton4.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont.png"))); // NOI18N
        samiButton4.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_dont.png"))); // NOI18N
        samiButton4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton4MouseClicked(evt);
            }
        });
        add(samiButton4, new org.netbeans.lib.awtextra.AbsoluteConstraints(90, 10, -1, -1));

        samiButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less 2.png"))); // NOI18N
        samiButton5.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less.png"))); // NOI18N
        samiButton5.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less 2.png"))); // NOI18N
        samiButton5.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less.png"))); // NOI18N
        samiButton5.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_less.png"))); // NOI18N
        samiButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton5MouseClicked(evt);
            }
        });
        add(samiButton5, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 10, -1, -1));

        samiButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search 2.png"))); // NOI18N
        samiButton1.setMouseEntered(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search.png"))); // NOI18N
        samiButton1.setMouseExited(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search 2.png"))); // NOI18N
        samiButton1.setMousePressed(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search.png"))); // NOI18N
        samiButton1.setMouseReleased(new javax.swing.ImageIcon(getClass().getResource("/Imagenes/Iconos/SAMI_search.png"))); // NOI18N
        samiButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                samiButton1MouseClicked(evt);
            }
        });
        add(samiButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(170, 10, -1, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void samiButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton2MouseClicked
         //Nuevo
        this.estadoEdicion();
        this.Limpiar();
        this.setEsNuevo(true);
    }//GEN-LAST:event_samiButton2MouseClicked

    private void samiButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton3MouseClicked
        //Guardar
         final SwingWorker worker = new SwingWorker() {
           
           @Override
           protected Object doInBackground() throws Exception {
            parent.getGlassPane().setVisible(true);
            if( isEsNuevo() && isTodoOK()){
                System.out.println(getTabla());
            if( getFT().Guardar(getTabla())){ 
                estadoNormal();
                        setEsNuevo(false);
                new Error(parent, true, "Registro Guardado Correctamente", 0, "Guardar").setVisible(true);
            }else{
                new Error(parent, true, "No se puede Almacenar el Registro", 0, "Error al Guardar").setVisible(true);
            }
        }
        else if(isTodoOK()){
            estadoNormal();
        }
            
            parent.getGlassPane().setVisible(false);
            return null;
            }
        };
         
       worker.execute();
        
    }//GEN-LAST:event_samiButton3MouseClicked

    private void samiButton4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton4MouseClicked
        //cancelar
        final SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                 estadoNormal();
                 return null;
            }
        };
       worker.execute();
    }//GEN-LAST:event_samiButton4MouseClicked

    private void samiButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton5MouseClicked
         // Eliminar
        final SwingWorker worker = new SwingWorker() {

            @Override
            protected Object doInBackground() throws Exception {
                
                String res= new Error(parent, true, "¿Desea Eliminar el Registro Seleccionado?", 1, "Eliminar").activar();
                parent.getGlassPane().setVisible(true);
                //getGlassPane().setVisible(true);
                if(res.equals("1"))
                    if(!FT.Eliminar(tabla)) new Error(parent, true, "No se puede Eliminar el Registro", 0, "Error al Guardar").setVisible(true);
                     else
                    estadoNormal();
                
                parent.getGlassPane().setVisible(false);
                //getGlassPane().setVisible(false);
                return null;
            }
        };
        
        worker.execute();
    }//GEN-LAST:event_samiButton5MouseClicked

    private void samiButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_samiButton1MouseClicked
        // Buscar
        int x=this.editor.getX();
        int y=this.editor.getY();
        this.getFiltro().setLocation(x, y);
        this.getFiltro().setVisible(true);
        this.editor.setVisible(false);
        
        
      //  System.out.println("no implementado aun");
    }//GEN-LAST:event_samiButton1MouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private Comun.SamiButton samiButton1;
    private Comun.SamiButton samiButton2;
    private Comun.SamiButton samiButton3;
    private Comun.SamiButton samiButton4;
    private Comun.SamiButton samiButton5;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the FT
     */
    public FuenteTabla getFT() {
        return FT;
    }

    /**
     * @param FT the FT to set
     */
    public void setFT(FuenteTabla FT) {
        this.FT = FT;
    }

    /**
     * @return the nav
     */
    public pnlNavegacion getNav() {
        return nav;
    }

    /**
     * @param nav the nav to set
     */
    public void setNav(pnlNavegacion nav) {
        this.nav = nav;
    }

    /**
     * @return the tabla
     */
    public String getTabla() {
        return tabla;
    }

    /**
     * @param tabla the tabla to set
     */
    public void setTabla(String tabla) {
        this.tabla = tabla;
    }

    /**
     * @return the esNuevo
     */
    public boolean isEsNuevo() {
        return esNuevo;
    }

    /**
     * @param esNuevo the esNuevo to set
     */
    public void setEsNuevo(boolean esNuevo) {
        this.esNuevo = esNuevo;
    }

    /**
     * @param parent the parent to set
     */
    public void setParent(JFrame parent) {
        this.parent = parent;
    }

    /**
     * @return the Vista
     */
    public SamiFrame getVista() {
        return Vista;
    }

    /**
     * @param Vista the Vista to set
     */
    public void setVista(SamiFrame Vista) {
        this.Vista = Vista;
    }

    /**
     * @return the editor
     */
    public PanelRoundTranslucido getEditor() {
        return editor;
    }

    /**
     * @param editor the editor to set
     */
    public void setEditor(PanelRoundTranslucido editor) {
        this.editor = editor;
    }

    /**
     * @return the filtro
     */
    public pnlFiltro getFiltro() {
        return filtro;
    }

    /**
     * @param filtro the filtro to set
     */
    public void setFiltro(pnlFiltro filtro) {
        this.filtro = filtro;
    }
}
